﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Drill.TestSupport.TestDomains.SimpleDomain;
using NUnit.Framework;

namespace Drill.TestSupport
{
	[TestFixture]
	public abstract class BaseDrillBitTests
	{
		protected abstract IDependencyResolver GetDependencyResolverInstanceToTest();

		[Test]
		public void CanDispose()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			dependencyResolver.Dispose();

			// Assert
			// Nothing to asset. An exception should not be thrown.
		}

		[ExpectedException(typeof(ObjectDisposedException), ExpectedMessage = "has been disposed", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotInvokeGetInstanceAfterDispose()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			dependencyResolver.Dispose();
			dependencyResolver.GetInstance(typeof(ITestObjA));

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[ExpectedException(typeof(ObjectDisposedException), ExpectedMessage = "has been disposed", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotInvokeGetInstanceGenericAfterDispose()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			dependencyResolver.Dispose();
			dependencyResolver.GetInstance<ITestObjA>();

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[ExpectedException(typeof(ObjectDisposedException), ExpectedMessage = "has been disposed", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotInvokeGetNamedInstanceAfterDispose()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			dependencyResolver.Dispose();
			dependencyResolver.GetInstance(typeof(ITestObjB), "MyObjB");

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[ExpectedException(typeof(ObjectDisposedException), ExpectedMessage = "has been disposed", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotInvokeGetNamedInstanceGenericAfterDispose()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			dependencyResolver.Dispose();
			dependencyResolver.GetInstance<ITestObjB>("MyObjB");

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[ExpectedException(typeof(ObjectDisposedException), ExpectedMessage = "has been disposed", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotInvokeGetAllInstancesAfterDispose()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			dependencyResolver.Dispose();
			dependencyResolver.GetAllInstances(typeof(ITestObjB));

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[ExpectedException(typeof(ObjectDisposedException), ExpectedMessage = "has been disposed", MatchType = MessageMatch.Contains)]
		[Test]
		public void CannotInvokeGetAllInstancesGenericAfterDispose()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			dependencyResolver.Dispose();
			dependencyResolver.GetAllInstances<ITestObjB>();

			// Assert
			// Nothing to assert because the test should throw an expected exception (see ExpectedException attribute).
		}

		[Test]
		public void CanGetInstance()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			ITestObjA testObjA = dependencyResolver.GetInstance(typeof(ITestObjA)) as ITestObjA;

			// Assert
			Assert.That(testObjA, Is.Not.Null);
			Assert.That(testObjA, Is.AssignableFrom<TestObjA>());
		}

		[Test]
		public void CanGetInstanceGeneric()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			ITestObjA testObjA = dependencyResolver.GetInstance<ITestObjA>();

			// Assert
			Assert.That(testObjA, Is.Not.Null);
			Assert.That(testObjA, Is.AssignableFrom<TestObjA>());
		}

		[Test]
		public void CanGetNamedInstance()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			ITestObjB myObjB = dependencyResolver.GetInstance(typeof(ITestObjB), "MyObjB") as ITestObjB;

			// Assert
			Assert.That(myObjB, Is.Not.Null);
			Assert.That(myObjB, Is.AssignableFrom<TestObjB>());
		}

		[Test]
		public void CanGetNamedInstanceGeneric()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			ITestObjB myObjB = dependencyResolver.GetInstance<ITestObjB>("MyObjB");

			// Assert
			Assert.That(myObjB, Is.Not.Null);
			Assert.That(myObjB, Is.AssignableFrom<TestObjB>());
		}

		[Test]
		public void CanGetAllInstances()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			IEnumerable enumerable = dependencyResolver.GetAllInstances(typeof(ITestObjB));
			List<ITestObjB> list = enumerable.Cast<ITestObjB>().ToList();

			// Assert
			Assert.That(list.Count, Is.EqualTo(2));
		}

		[Test]
		public void CanGetAllInstancesGeneric()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			// Act
			IEnumerable<ITestObjB> enumerable = dependencyResolver.GetAllInstances<ITestObjB>();
			List<ITestObjB> list = enumerable.ToList();

			// Assert
			Assert.That(list.Count, Is.EqualTo(2));
		}

		[Test]
		public void CanGetInstanceMultipleTimes()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			for (int i = 0; i < 100; i++)
			{
				// Act
				ITestObjA testObjA = dependencyResolver.GetInstance(typeof(ITestObjA)) as ITestObjA;

				// Assert
				Assert.That(testObjA, Is.Not.Null);
				Assert.That(testObjA, Is.AssignableFrom<TestObjA>());
			}
		}

		[Test]
		public void CanGetInstanceGenericMultipleTimes()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			for (int i = 0; i < 100; i++)
			{
				// Act
				ITestObjA testObjA = dependencyResolver.GetInstance<ITestObjA>();

				// Assert
				Assert.That(testObjA, Is.Not.Null);
				Assert.That(testObjA, Is.AssignableFrom<TestObjA>());
			}
		}

		[Test]
		public void CanGetNamedInstanceMultipleTimes()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			for (int i = 0; i < 100; i++)
			{
				// Act
				ITestObjB myObjB = dependencyResolver.GetInstance(typeof(ITestObjB), "MyObjB") as ITestObjB;

				// Assert
				Assert.That(myObjB, Is.Not.Null);
				Assert.That(myObjB, Is.AssignableFrom<TestObjB>());
			}
		}

		[Test]
		public void CanGetNamedInstanceGenericMultipleTimes()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			for (int i = 0; i < 100; i++)
			{
				// Act
				ITestObjB myObjB = dependencyResolver.GetInstance<ITestObjB>("MyObjB");

				// Assert
				Assert.That(myObjB, Is.Not.Null);
				Assert.That(myObjB, Is.AssignableFrom<TestObjB>());
			}
		}

		[Test]
		public void CanGetAllInstancesMultipleTimes()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			for (int i = 0; i < 100; i++)
			{
				// Act
				IEnumerable enumerable = dependencyResolver.GetAllInstances(typeof(ITestObjB));
				List<ITestObjB> list = enumerable.Cast<ITestObjB>().ToList();

				// Assert
				Assert.That(list.Count, Is.EqualTo(2));
			}
		}

		[Test]
		public void CanGetAllInstancesGenericMultipleTimes()
		{
			// Arrange
			IDependencyResolver dependencyResolver = GetDependencyResolverInstanceToTest();

			for (int i = 0; i < 100; i++)
			{
				// Act
				IEnumerable<ITestObjB> enumerable = dependencyResolver.GetAllInstances<ITestObjB>();
				List<ITestObjB> list = enumerable.ToList();

				// Assert
				Assert.That(list.Count, Is.EqualTo(2));
			}
		}
	}
}